package com.wg.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.wg.api.domain.PollutionbeanDO;
import com.wg.api.domain.PollutionsDO;
import com.wg.api.service.DataShowService;
import com.wg.common.utils.PageUtils;
import com.wg.common.utils.Query;
import com.wg.common.utils.R;

/**
 * 
 * 
 * @author Plum
 * @email blackplum@vip.qq.com
 * @date 2019-05-10 16:04:27
 */
 
@Controller
@RequestMapping("/api/dataShow")
public class DataShowController {
	@Autowired
	private DataShowService dataShowService;
	
	@GetMapping()
	String Pollutions(){
	    return "api/pollutions/pollutions";
	}
	
	@GetMapping("/singleStationRecentShow")
	@ResponseBody
	List<PollutionsDO> singleStationRecentShow(String stationNo){
		Map<String, Object> params = new HashMap<>();
		params.put("offset", 0);
		params.put("limit", 100);
		params.put("stationNo", stationNo);
		Query query = new Query(params);
		List<PollutionsDO> pollutionsList = dataShowService.list(query);
		return pollutionsList;
	}
	
	@GetMapping("/multiStationRecentShow")
	@ResponseBody
	String multiStationRecentShow( Integer id){
		Map<String, Object> params = new HashMap<>();
		params.put("offset", 1);
		params.put("limit", 10);
		Query query = new Query(params);
		List<PollutionsDO> pollutionsList = dataShowService.list(query);
		return JSON.toJSONString(pollutionsList);
	}
	
	
	//数据查询
	@PostMapping("/historyData")
	@ResponseBody
	String historyData(@RequestBody  Map<String, Object> param){
		Map<String, Object> params = new HashMap<>();
		params.put("offset", 1);
		params.put("limit", 100);
		params.put("citys", param.get("citys"));
		params.put("startTime", param.get("startTime"));
		params.put("endTime", param.get("endTime"));
		Query query = new Query(params);
		List<PollutionbeanDO> pollutionsList = dataShowService.historyData(query);
		return JSON.toJSONString(pollutionsList);
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<PollutionsDO> pollutionsList = dataShowService.list(query);
		int total = dataShowService.count(query);
		PageUtils pageUtils = new PageUtils(pollutionsList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "api/pollutions/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id,Model model){
		PollutionsDO pollutions = dataShowService.get(id);
		model.addAttribute("pollutions", pollutions);
	    return "api/pollutions/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( PollutionsDO pollutions){
		if(dataShowService.save(pollutions)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@ResponseBody
	public R update( PollutionsDO pollutions){
		dataShowService.update(pollutions);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer id){
		if(dataShowService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] ids){
		dataShowService.batchRemove(ids);
		return R.ok();
	}
	
}
